package com.ramos.kuaixiao.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.ramos.kuaixiao.entity.SaleCustomer;

import java.util.ArrayList;
import java.util.List;

public class SaleCustomerDB {
    private static String TAG = "SaleCustomerDB";
    private static String TABLE_NAME = "SaleCustomer";
    public static final String CREATE_TABLE_SQL;
    public static final String DROP_USERINFO;

    static {
        CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + " (" + "customerId" + "  CHAR(30) PRIMARY KEY  NOT NULL,"
                + "dealerId" + " CHAR(30)," + "saleId" + " CHAR(30)," +  "stopUseFlag "+ "  CHAR(30),"+"modifyTime" + " Long);";
        DROP_USERINFO = "DROP TABLE IF EXISTS " + TABLE_NAME;
    }

    public int addAllCustomer(List<SaleCustomer> customer) {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM SaleCustomer");
        String sql = "insert into SaleCustomer(customerId,dealerId,saleId,stopUseFlag,modifyTime) values(?,?,?,?,?)";
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        SQLiteStatement stmt = db.compileStatement(sql);
        for (int i = 0; i < customer.size(); i++) {
            stmt.bindString(1, customer.get(i).getCustomerId() + "");
            stmt.bindString(2, customer.get(i).getDealerId() + "");
            stmt.bindString(3, customer.get(i).getSaleId() + "");
            stmt.bindString(4, customer.get(i).getStopUseFlag() + "");
            stmt.bindString(5, customer.get(i).getModifyTime() + "");
            stmt.execute();
            stmt.clearBindings();
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }

    public int updateAllSaleCustomer(List<SaleCustomer> customer) {
        SQLiteDatabase db = DBHelper.open();
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        Log.d(TAG, "updateAllSaleCustomer:" + customer.size());
        for (int i = 0; i < customer.size(); i++) {
            SaleCustomer firstWarehouse = customer.get(i);
            ContentValues cv = new ContentValues();
            cv.put("customerId", firstWarehouse.getCustomerId() + "");
            cv.put("dealerId", firstWarehouse.getDealerId() + "");
            cv.put("saleId", firstWarehouse.getSaleId() + "");
            cv.put("stopUseFlag", firstWarehouse.getStopUseFlag() + "");
            cv.put("modifyTime", firstWarehouse.getModifyTime() + "");
            db.insertWithOnConflict("SaleCustomer", null, cv, SQLiteDatabase.CONFLICT_REPLACE); //如果有记录，则更新，并返回1，否则返回0
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }

    public List<SaleCustomer> findAllCustomer() {
        List<SaleCustomer> list = new ArrayList<>();
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select * from "+ TABLE_NAME, new String[]{});
        Log.d(TAG, "cursor:" + cursor.getCount());
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();
            if(cursor.getString(3).equals("F")) {
                SaleCustomer customer = new SaleCustomer();
                customer.setCustomerId(cursor.getString(0));
                customer.setDealerId(cursor.getString(1));
                customer.setSaleId(cursor.getString(2));
                customer.setStopUseFlag(cursor.getString(3));
                //customer.setModifyTime(Long.valueOf(cursor.getString(4)));
                list.add(customer);
            }
        }
        cursor.close();
        Log.d(TAG, "findAllCustomer:" + list.size());
        return list;
    }

    public long getMaxModifyTime() {
        long param = 0;
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select max(modifyTime)from SaleCustomer ", new String[]{});
        Log.d(TAG, "getMaxModifyTime:getCount" + cursor.getCount());
        if (cursor.moveToFirst()) {
            param = cursor.getLong(0);
        }
        cursor.close();
        Log.d(TAG, "getMaxModifyTime:param" + param);
        return param;
    }

    public void deleteAll() {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM SaleCustomer");
        Log.d(TAG, "deleteAllSaleCustomer ");
    }

    public boolean hasData() {
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select count(*)from SaleCustomer", null);
        cursor.moveToFirst();
        Long count = cursor.getLong(0);
        if (count > 0) {
            cursor.close();
            Log.d(TAG, "SaleCustomer true");
            return true;
        } else {
            Log.d(TAG, "SaleCustomer false");
            cursor.close();
            return false;
        }
    }

}
